package com.cupcorn.balapkarung.entity;

import org.andengine.entity.Entity;

import com.cupcorn.balapkarung.GameDefineText;
import com.cupcorn.balapkarung.LocalData;
import com.cupcorn.balapkarung.state.StateGamePlay;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameAutoParallax;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameRectangle;
import com.cupcorn.onlinescript.ObjectScoring;
import com.cupcorn.onlinescript.OnlineScript;

public class World
{
	float				widthCollition;
	float				heightCollition;
	GameEngine			engine;
	GameAutoParallax	parallax;
	MC[]				mc;
	Map[]				map;
	GameRectangle[][]	recGround;
	GameRectangle[][]	recGameOver;
	GameRectangle[][]	recDump;
	GameRectangle[]		recMap;
	StateGamePlay		gamePlay;
	
	public World(GameEngine engine, StateGamePlay gamePlay, GameAutoParallax parallax, MC[] mc, Map... map)
	{
		this.engine = engine;
		this.gamePlay = gamePlay;
		this.parallax = parallax;
		this.mc = mc;
		this.map = map;
		recGround = new GameRectangle[map.length][map[0].ground.length];
		recGameOver = new GameRectangle[map.length][map[0].gameOver.length];
		recDump = new GameRectangle[map.length][map[0].dump.length];
		recMap = new GameRectangle[map.length];
		for (int i = 0; i < map.length; i++)
		{
			this.recGround[i] = map[i].ground;
			this.recGameOver[i] = map[i].gameOver;
			this.recDump[i] = map[i].dump;
			this.recMap[i] = map[i].recMap;
		}
	}
	
	public void onUpdate(float deltaTime)
	{
		for (int i = 0; i < StateGamePlay.MAX_MC_COUNT; i++)
		{
			if (mc[i].curState != mc[i].DIE)
			{
				if (mc[i].mc.getY() > engine.camera.getYMax())
				{
					if (i == 0)
						gameOver(i);
					else
						mc[i].switchState(mc[i].DIE);
					
					return;
				}
				mc[i].isInGround = false;
				for (int j = 0; j < map.length; j++)
				{
					if (Utils.isOnScreen(engine, recMap[j]))
					{
						for (int k = 0; k < recGround[j].length; k++)
						{
							if (Utils.isOnScreen(engine, recGround[j][k]))
							{
								if (mc[i].mc.collidesWith(recGround[j][k]))
								{
									widthCollition = mc[i].mc.getX() + mc[i].mc.getWidth() - recGround[j][k].getX();
									heightCollition = mc[i].mc.getY() + mc[i].mc.getHeight() - recGround[j][k].getY();
									
									if (widthCollition < heightCollition)
									{
										if (i == 0)
										{
											gameOver(i);
											return;
										}
										else
										{
											mc[i].switchState(mc[i].DIE);
											return;
										}
									}
									else
									{
										onGround(i, recGround[j][k]);
									}
									
								}
							}
						}
						
						for (int k = 0; k < recDump[j].length; k++)
						{
							if (Utils.isOnScreen(engine, recDump[j][k]))
							{
								if (mc[i].mc.collidesWith(recDump[j][k]))
								{
									widthCollition = mc[i].mc.getX() + mc[i].mc.getWidth() - recDump[j][k].getX();
									heightCollition = mc[i].mc.getY() + mc[i].mc.getHeight() - recDump[j][k].getY();
									if (widthCollition < heightCollition)
									{
										if (i == 0)
										{
											gameOver(i);
											return;
										}
										else
										{
											mc[i].switchState(mc[i].DIE);
											return;
										}
									}
									else
									{
										onGround(i, recDump[j][k]);
									}
								}
							}
						}
					}
				}
				if (!mc[i].isInGround && mc[i].curState != mc[i].JUMP && mc[i].curState != mc[i].DOWN)
				{
					mc[i].curGravitation += mc[i].GRAVITATION * deltaTime;
					
					mc[i].mc.setPosition(mc[i].mc.getX(), mc[i].mc.getY() + mc[i].curGravitation * deltaTime);
				}
			}
		}
	}
	
	private void gameOver(int index)
	{
		if (LocalData.highscoreOnline < StateGamePlay.score && StateGamePlay.isOnline)
		{
			LocalData.highscoreOnline = StateGamePlay.score;
			LocalData.listReplayJump.add(LocalData.listJump);
			ObjectScoring objectScore = new ObjectScoring(Utils.getUsername(), LocalData.listJump, StateGamePlay.score);
			OnlineScript.sendData(objectScore);
		}
		parallax.setParallaxChangePerSecond(0);
		mc[index].switchState(mc[index].DIE);
		gamePlay.displayText.text[GameDefineText.TEXT_SCORE_MOVE].setText("");
		gamePlay.displayText.text[GameDefineText.TEXT_SCORE_MOVE].setPosition(gamePlay.xTextStart, gamePlay.yTextStart);
	}
	
	private void onGround(int index, Entity gameSprite)
	{
		mc[index].isInGround = true;
		mc[index].curGravitation = 0;
		mc[index].curJump = 0;
		mc[index].mc.setPosition(mc[index].mc.getX(), gameSprite.getY() - mc[index].mc.getHeight() + 2);
		if (mc[index].curState != mc[index].MOVE) mc[index].switchState(mc[index].MOVE);
	}
}